package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.behavior.service.mq.ApUserOperationsService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollectionBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author XHui
 * @Since 2024/3/3 16:48
 * @Version 1.0
 */
@Service
public class ApCollectionBehaviorServiceImpl implements ApCollectionBehaviorService {
    
    @Resource
    private MongoTemplate mongoTemplate;
    
    @Resource
    private ApBehaviorEntryService apBehaviorEntryService; 

    @Resource
    private ApUserOperationsService apUserOperationsService;

    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        ApBehaviorEntry entry = apBehaviorEntryService.getApBehaviorEntryByUserIdOrEquipmentId(dto.getEquipmentId());

        // 1. 根据articleId查询该文章的不收藏行为数据
        // 1.1 查询 行为实体 与 文章操作 的关联关系
        ApCollectionBehavior apCollectionBehavior = apBehaviorEntryService.findBehaviorTraceByEntryIdAndArticleId(
                dto.getArticleId(), dto.getEquipmentId(), ApCollectionBehavior.class
        );

        // 2. 若行为实体 不收藏该文章的记录不存在， 只能进行不收藏
        Short type = dto.getType();
        Short operation = dto.getOperation();

        // 2.1 发送操作数据到rabbitmq
        boolean sendToRabbitFlag = true;
        NewBehaviorDTO newBehaviorDTO = new NewBehaviorDTO();
        newBehaviorDTO.setType(NewBehaviorDTO.BehaviorType.COLLECTION);
        newBehaviorDTO.setArticleId(dto.getArticleId());

        if(apCollectionBehavior == null &&
                operation == ApCollectionBehavior.Operation.COLLECTION.getCode() ){

            newBehaviorDTO.setAdd(1);

            // 2.1 新增不收藏的记录
            apCollectionBehavior = new ApCollectionBehavior();
            apCollectionBehavior.setEntryId(entry.getId());
            apCollectionBehavior.setArticleId(dto.getArticleId());
            apCollectionBehavior.setType(type);
            apCollectionBehavior.setPublishedTime(dto.getPublishedTime());
            apCollectionBehavior.setCollectionTime(new Date());

            // 2.2 插入收藏记录
            mongoTemplate.insert(apCollectionBehavior);
        } else if (apCollectionBehavior != null &&
                operation == ApCollectionBehavior.Operation.CANCEL_COLLECTION.getCode() ) {
            newBehaviorDTO.setAdd(-1);
            // 3. 若行为实体 不收藏该文章的记录存在， 只能进行取消不收藏
            mongoTemplate.remove(apCollectionBehavior);
        } else {
            sendToRabbitFlag = false;
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW, "收藏操作不合法");
        }

        if(sendToRabbitFlag){
            apUserOperationsService.sendUserOperationsToRabbitMQ(newBehaviorDTO);
        }
        // 4. 返回结果
        return ResponseResult.okResult();
    }

}
